home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / util / misc / NVX_timeul2.lha / C_Source.lha / Lists.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  5KB  |  219 lines

  1. ;/* Lists - Execute me to compile me with Lattice.
  2. LC -b0 -cfistq -v -y Lists.c
  3. Blink with Make
  4. quit
  5. */
  6.  
  7. /*** ******************************** ***/
  8. /***                                  ***/
  9. /***  Generic List Handling Routines  ***/
  10. /***                                  ***/
  11. /***                 by STIGG // NVX  ***/
  12. /***                                  ***/
  13. /***            Version 1.0              ***/
  14. /***                                  ***/
  15. /*** ******************************** ***/
  16.  
  17.  
  18.  
  19. #include "stdio.h"
  20. #include "string.h"
  21. #include "ctype.h"
  22. #include <exec/exec.h>
  23. #include <exec/lists.h>
  24. #include <exec/nodes.h>
  25. #include <dos/dos.h>
  26. #include <dos/dosextens.h>
  27. #include <proto/exec.h>
  28. #include <proto/dos.h>
  29.  
  30. /* Expand this structure to suit specific program */
  31.  
  32. struct MyNode
  33.     {
  34.     struct    Node    mn_Node;    /* A node structure */
  35.     APTR            mn_Entry;    /* Reference Pointer */
  36.     };
  37.  
  38. /* Function Prototypes */
  39.  
  40. struct Node *AddANodeAlpha( struct List *list, char *Name );
  41. void ClearList( struct List *Start );
  42. BOOL FindNodeA( struct Node *Start, char *String, struct Node **Insert );
  43. struct Node *FindOrd( struct List *list, UWORD ordinal );
  44. WORD OrdNode( struct Node *node );
  45. void RemoveANode( struct Node *Bye );
  46. APTR Allocvec( ULONG size, LONG type );
  47. void Freevec( APTR addr );
  48.  
  49.     /* ******************************************************** */
  50.     /*                     Add A Node Alphabetically                */
  51.     /* ******************************************************** */
  52.  
  53. struct Node *AddANodeAlpha( struct List *list, char *Name )
  54. {
  55.  
  56. struct Node *NewNode,*temp,*Posn;
  57.  
  58. /* Allocate memory for node */
  59.  
  60. if ( NewNode=(struct Node *)Allocvec( sizeof(struct MyNode), MEMF_CLEAR))
  61.     {
  62.     /* Allocate memory for copy of title */
  63.     
  64.     if ( NewNode->ln_Name=(char *)Allocvec( 1+strlen(Name), MEMF_CLEAR))
  65.         {
  66.         strcpy( NewNode->ln_Name, Name );
  67.  
  68.         /* Add this node to the list after the specified node */
  69.  
  70.         FindNodeA( (struct Node *)list, Name, &Posn );
  71.         
  72.         NewNode->ln_Succ=Posn->ln_Succ;
  73.         NewNode->ln_Pred=Posn;
  74.         temp=(struct Node *)Posn->ln_Succ;
  75.         Posn->ln_Succ=NewNode;
  76.         temp->ln_Pred=NewNode;
  77.         
  78.         }
  79.     else
  80.         {
  81.         Freevec(NewNode);
  82.         NewNode=NULL;
  83.         }
  84.     }
  85. return NewNode;
  86. }
  87.  
  88.     /* ******************************************************** */
  89.     /*                 Clear All Nodes From A List                    */
  90.     /* ******************************************************** */
  91.  
  92. void ClearList( struct List *Start )
  93. {
  94. struct Node *this_node;
  95. struct Node *next_node;
  96.  
  97. if( Start )
  98.     {
  99.     this_node=((struct Node *)Start)->ln_Succ;  /* -> to 1st node */
  100.  
  101.     while( next_node=(struct Node *)( this_node->ln_Succ ) )
  102.         {
  103.         if( this_node->ln_Name ) Freevec( this_node->ln_Name );
  104.         Freevec( this_node );
  105.         this_node=next_node;
  106.         }
  107.     NewList( Start );
  108.     }
  109. }
  110.  
  111.     /* ******************************************************** */
  112.     /*                     Find A Node Given Name                    */
  113.     /* ******************************************************** */
  114.  
  115. /* Returns TRUE if node found, returns pointer to node with name ordinaly
  116.    lower than one we require for insertion */
  117.  
  118. BOOL FindNodeA( struct Node *Start, char *String, struct Node **Insert )
  119. {
  120.  
  121. int i;
  122. BOOL RetVal=FALSE;
  123.  
  124. *Insert=Start;
  125. while( (Start->ln_Succ)->ln_Succ )
  126.     {
  127.     Start=(struct Node *)( Start->ln_Succ );
  128.     i=strcmpi( String, Start->ln_Name );
  129.     if ( !i ) RetVal=TRUE;
  130.     if( i>=0 ) *Insert=Start;
  131.     }
  132.  
  133. return RetVal;
  134. }
  135.  
  136.     /* ******************************************************** */
  137.     /*                 Find A Node Given Ordinal Number            */
  138.     /* ******************************************************** */
  139.  
  140. struct Node *FindOrd( struct List *list, UWORD ordinal )
  141. {
  142.  
  143. struct Node *node;
  144. UWORD i;
  145.  
  146. node = (struct Node *)list;
  147. ordinal++;
  148.  
  149. for( i=0; (i<ordinal) && (node); i++ )
  150.     node = node->ln_Succ;
  151.  
  152. if( node->ln_Succ==NULL )node=NULL;
  153.  
  154. return node;
  155. }
  156.  
  157.     /* ******************************************************** */
  158.     /*             Return Ordinal Number of Given Node                */
  159.     /* ******************************************************** */
  160.  
  161. /* Will return ordinal number of given node, -1 for start of list */
  162.  
  163. WORD OrdNode( struct Node *node )
  164. {
  165.  
  166. register WORD ord=-1;
  167.  
  168. while( node = node->ln_Pred ) ord++;
  169. return ord;
  170. }
  171.  
  172.     /* ******************************************************** */
  173.     /*                 Remove And Free A Node                        */
  174.     /* ******************************************************** */
  175.  
  176. void RemoveANode( struct Node *Bye )
  177. {
  178.  
  179. ((struct Node *)(Bye->ln_Pred))->ln_Succ=Bye->ln_Succ;
  180. ((struct Node *)(Bye->ln_Succ))->ln_Pred=Bye->ln_Pred;
  181.  
  182. if( Bye->ln_Name ) Freevec( Bye->ln_Name );
  183. Freevec( Bye );
  184.  
  185. }
  186.  
  187.     /* ******************************************************** */
  188.     /*         Memory Allocation Routines Missing From WB1.3        */
  189.     /* ******************************************************** */
  190.  
  191. /* The following two routines have been written so that some of my code
  192.    written for WB2+ machines will run on WB1.3 as well. These are direct
  193.    substitutes for AllocVec() and FreeVec(), plug in and go.... */
  194.  
  195. APTR Allocvec( ULONG size, LONG type )
  196. {
  197. ULONG *buf;
  198.  
  199. size += 4;            /* allow for our magic bit */
  200.  
  201. if( buf=(ULONG *)AllocMem( size, type ) ) *buf++=size;
  202.  
  203. return (APTR)buf;
  204. }
  205.  
  206. void Freevec( APTR addr )
  207. {
  208. LONG *buf;
  209. ULONG size;
  210.  
  211. if( addr )
  212.     {
  213.     buf = (LONG *)addr;
  214.     buf--;
  215.     size = *buf;
  216.     FreeMem( (APTR)buf, size );
  217.     }
  218. }
  219.